/*----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the 
License at http://www.mozilla.org/MPL/
	
Software distributed under the License is distributed on 
an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, 
either express or implied. 
See the License for the specific language governing rights and 
limitations under the License.
	
The Original Code is Fever RS ( ActionScript 3 ) Framework code.
	
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
	
Contributor(s):
	
Unless required by applicable law or agreed to in writing, 
software distributed under the License is distributed on 
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied.
See the License for the specific language governing permissions 
and limitations under the License.
----------------------------------------------------------------------------*/

package fever.load 
{
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	import com.bourre.load.AbstractLoader;
	import com.bourre.load.LoaderEvent;
	import com.bourre.load.XMLLoaderDeserializer;

	import fever.load.strategy.CipherLoaderStrategy;	

	
	/**
	 * Dispatched when the loading start.
	 *
	 * @eventType fever.load.CipherXMLLoaderEvent.onLoadStartEVENT
	 */
	[Event(name="onLoadStart", type="fever.load.CipherXMLLoaderEvent")]

	/**
	 * Dispatched during loading progression.
	 *
	 * @eventType fever.load.CipherXMLLoaderEvent.onLoadProgressEVENT
	 */
	[Event(name="onLoadProgress", type="fever.load.CipherXMLLoaderEvent")]

	/**
	 * Dispatched when the loading complete.
	 *
	 * @eventType fever.load.CipherXMLLoaderEvent.onLoadInitEVENT
	 */
	[Event(name="onLoadInit", type="fever.load.CipherXMLLoaderEvent")]

	/**
	 * Dispatched when an error occured during loading process.
	 *
	 * @eventType fever.load.CipherXMLLoaderEvent.onLoadErrorEVENT
	 */
	[Event(name="onLoadError", type="fever.load.CipherXMLLoaderEvent")]

	/**
	 * Dispatched on loading timeout.
	 *
	 * @eventType fever.load.CipherXMLLoaderEvent.onLoadTimeOutEVENT
	 */
	[Event(name="onLoadTimeOut", type="fever.load.CipherXMLLoaderEvent")]

	/**
	 * Loads xml crypted file.
	 * 
	 * fever.load.strategy.CipherLoaderStrategy
	 * 
	 * @author Romain Ecarnot
	 */
	public class CipherXMLLoader extends AbstractLoader
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------

		private var _oDeserializer : XMLLoaderDeserializer;

		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Constructor.
		 * 
		 * @param cryptAlgo Algorithm to use for decrypting loaded data
		 * @param secretKey Secret key to use for decrypting loaded data
		 * @param deserializer XML to Object deserializer
		 * 
		 * @see fever.data.crypto.Cipher
		 * @see fever.data.crypto.CryptAlgorithm
		 */
		public function CipherXMLLoader( cryptAlgo : Class,	secretKey : String, deserializer : XMLLoaderDeserializer = null )
		{
			super( new CipherLoaderStrategy( cryptAlgo, secretKey ) );
			
			if ( deserializer ) setDeserializer( deserializer );
		}

		/**
		 * Sets the XML Deserializer to use.
		 * 
		 * @param deserializer XML Deserializer instance
		 */
		public function setDeserializer( deserializer : XMLLoaderDeserializer ) : void
		{
			_oDeserializer = deserializer;
		}

		/**
		 * Returns used XML Deserializer.
		 */
		public function getDeserializer() : XMLLoaderDeserializer 
		{
			return _oDeserializer;
		}

		/**
		 * Returns loader event.
		 */
		protected override function getLoaderEvent( type : String ) : LoaderEvent
		{
			return new CipherXMLLoaderEvent( type, this );
		}

		/**
		 * Returns XML Content.
		 */
		public function getXML() : XML
		{
			return XML( getContent( ) );
		}

		/**
		 * Loads xml file defined in <code>url</code> request.
		 * 
		 * @param url XML file url
		 * @param context Loader context
		 */
		public override function load( url : URLRequest = null, context : LoaderContext = null ) : void
		{
			release( );

			super.load( url, context );
		}

		
		//--------------------------------------------------------------------
		// Private implementations
		//--------------------------------------------------------------------
				
		/**
		 * @priva
		
		
		te
		 */
		protected override function onInitialize() : void
		{
			if ( getDeserializer( ) != null ) getDeserializer( ).deserialize( new Object( ), getXML( ) );
			
			super.onInitialize( );
		}
	}
}
